package org.ithang;

import java.io.File;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

import javax.swing.JFileChooser;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class GenBoostrap {

	private static String CurrentDir=null;
	public static void main(String[] args) {
		System.out.println( System.getProperty("user.home") + "\\Desktop");
		try {
			ProtectionDomain pd=GenBoostrap.class.getProtectionDomain();
			CodeSource src=pd.getCodeSource();
			URL url=src.getLocation();
			CurrentDir=url.toURI().getPath();
			System.out.println("path="+CurrentDir);
		}catch(Exception e) {
			e.printStackTrace();
		}
		//GenBoostrap.class.getClassLoader().getResource(".").getFile();
		
		if(null!=CurrentDir&&CurrentDir.trim().length()>0&&CurrentDir.startsWith("/")) {
			CurrentDir=CurrentDir.substring(1);
			File file=new File(CurrentDir);
			if(!file.isDirectory()) {
				CurrentDir=file.getParent();
			}
		}
		//log.info("当前目录:{}",CurrentDir);
		JFileChooser filechooser = new JFileChooser();
		filechooser = new JFileChooser(".");//当前路径文件
		filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		filechooser.setMultiSelectionEnabled(false);
		filechooser.setDialogTitle("选择一个excel文件");
		int state=filechooser.showOpenDialog(null);
		if(state==JFileChooser.APPROVE_OPTION) {
			System.out.println("选择文件");
			File file=filechooser.getSelectedFile();
			System.out.println("选择文件为："+file.getAbsolutePath());
			readData(file);
			//EasyExcel.read(file, Map.class, new OrderDataListener()).sheet().doRead();
			/*
			List<Order> data = EasyExcel.read(file).head(Order.class).sheet().doReadSync();
		    if(null!=data) {
		    	data.forEach(d->{
		    		System.err.println(d.toString());
		    	});
		    }
		    */
		}else {
			System.out.println("未选择文件");
		}

	}
	
	public static String getCurrentDir() {
		return GenBoostrap.CurrentDir;
	}
	
	public static void readData(File file) {
		try{
			XSSFWorkbook workbook = new XSSFWorkbook(file);
	        //2、获取工作表
	        XSSFSheet sheet = workbook.getSheetAt(0);
	        System.out.println("top="+sheet.getLastRowNum());
	        //3、获取行
	           for(int i=1;i<=sheet.getLastRowNum();i++) {
	        	   Order o=new Order();
	        	   o.setId(sheet.getRow(i).getCell(0).getStringCellValue());
	        	   o.setTitle(sheet.getRow(i).getCell(1).getStringCellValue());
	        	   o.setNumber(sheet.getRow(i).getCell(2).getStringCellValue());
	        	   o.setPid(sheet.getRow(i).getCell(3).getStringCellValue());
	        	   
	        	   try {
	        	       o.setNum(Integer.parseInt(sheet.getRow(i).getCell(4).getRawValue()));
	        	   }catch(Exception e) {
	        		   e.printStackTrace();
	        	   }
	        	   o.setUnit(sheet.getRow(i).getCell(5).getStringCellValue());
	        	   o.setAddress(sheet.getRow(i).getCell(6).getStringCellValue());
	        	   o.setPeople(sheet.getRow(i).getCell(7).getStringCellValue());
	        	   o.setPhone(sheet.getRow(i).getCell(8).getStringCellValue());
	        	   o.setToDate(sheet.getRow(i).getCell(9).getStringCellValue());
	        	   o.setVerifyDate(sheet.getRow(i).getCell(10).getStringCellValue());
	        	   o.setToAddress(sheet.getRow(i).getCell(11).getStringCellValue());
	        	   o.setOrderNum(sheet.getRow(i).getCell(12).getStringCellValue());
	        	   o.setOrderSubNum(sheet.getRow(i).getCell(13).getStringCellValue());
	        	   GenWord.genWord(o);
	           }
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

}
